<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-3-18
 * Time: 上午11:28
 */
namespace Service\Controller;
use Mcit\Page;
//用户设置
class SetController extends BaseController {

    public function index(){
        $user = M("operation")->where("oper_login_name='".$_SESSION['oper_login_name']."'")->find();
        //将数字的地址转换成文字
        $region = strsToArray($user['location']);
        $regionOne = D("Region")->where("code='".$region[1]."'")->field("fullname")->find();
        $user['location_text'] = $regionOne['fullname'];
        $this->user =$user;

        if(IS_POST){

            $logodirs  = strsToArrays($user['logo']);
            $logodir   = APP_ROOT_PATH."./Public/".$logodirs[1]."/".$logodirs[2]."/".$logodirs[3]."/".$logodirs[4];
            $portraitsdirs = strsToArrays($user['portrait']);
            $portraitdir  = APP_ROOT_PATH."./Public/".$portraitsdirs[1]."/".$portraitsdirs[2]."/".$portraitsdirs[3]."/".$portraitsdirs[4];
            if($_FILES['logo']['name'] != ''){
                $config = array(
                    'maxSize'       =>  3145728, //上传的文件大小限制 (0-不做限制)
                    'exts'          =>  array('jpg', 'gif', 'png', 'jpeg'), //允许上传的文件后缀
                    'autoSub'       =>  true, //自动子目录保存文件
                    'subName'       =>  array('date', 'Ymd'), //子目录创建方式，[0]-函数名，[1]-参数，多个参数使用数组
                    'rootPath'      =>     './Public/', //保存根路径
                    'savePath'      =>  './data/upload/faces/', //保存路径
                    'saveName'      =>  array('uniqid', ''), //上传文件命名规则，[0]-函数名，[1]-参数，多个参数使用数组
                );
                $upload = new \Think\Upload($config); // 实例化上传类
                $info = $upload->uploadOne($_FILES['logo']);
                if (!$info) { // 上传错误提示错误信息
//                    $this->error($upload->getError());
                } else { // 上传成功 获取上传文件信息
                    $logo     = $info[savepath].$info[savename];
                    if(file_exists(APP_ROOT_PATH."./Public/".$user['logo'])){
                        unlink(APP_ROOT_PATH."./Public/".$user['logo']);
                    }
                    rmdir($logodir);
                }
            }
            if($_FILES['portrait']['name'] != ''){
                $configs = array(
                    'maxSize'       =>  3145728, //上传的文件大小限制 (0-不做限制)
                    'exts'          =>  array('jpg', 'gif', 'png', 'jpeg'), //允许上传的文件后缀
                    'autoSub'       =>  true, //自动子目录保存文件
                    'subName'       =>  array('date', 'YmdHis'), //子目录创建方式，[0]-函数名，[1]-参数，多个参数使用数组
                    'rootPath'      =>     './Public/', //保存根路径
                    'savePath'      =>  './data/upload/faces/', //保存路径
                    'saveName'      =>  array('uniqid', ''), //上传文件命名规则，[0]-函数名，[1]-参数，多个参数使用数组
                );
                $uploads = new \Think\Upload($configs); // 实例化上传类
                $infos = $uploads->uploadOne($_FILES['portrait']);
                if (!$infos) { // 上传错误提示错误信息
//                    $this->error($uploads->getError());
                } else { // 上传成功 获取上传文件信息
                    $portrait = $infos[savepath].$infos[savename];
                    if(file_exists(APP_ROOT_PATH."./Public/".$user['portrait'])){
                        unlink(APP_ROOT_PATH."./Public/".$user['portrait']);
                    }
                    rmdir($portraitdir);
                }
            }
            $data['background']     = I("background");
            $data['color']          = I("color");
            $data['logo']           = $logo;
            $data['portrait']       = $portrait;
            $data['oper_name']      = I("oper_name");
            $data['address']        = I("address");
            $map['id']  = session('uid');
//            $result = M('operation')->where($map)->save($data);
            $sql = "update ev_operation set oper_name='".$data['oper_name']."',address='".$data['address']."',logo='".$data['logo']."',portrait='".$data['portrait']."',background='".$data['background']."',color='".$data['color']."' where id='".$map['id']."'";
            $result = M()->query($sql);
            if($result > 0){
                $return['state'] = 100;
                $return['msg']   = '修改成功';
                $return['url']   = U('Login/logout');
            }else{
                $return['state'] = 200;
                $return['msg']   = "修改失败";
            }
            echo json_encode($return);
            exit;
        }
        $this->display();
    }
    //修改密码
    public function changePwd(){
        $user = M("operation")->where("oper_login_name='".$_SESSION['oper_login_name']."'")->find();
        if(IS_POST){
            $old_password  = sha1(I('old_password'));
            $password  = I('oper_password');
            $password1 = I('oper_password1');

            if($user['oper_password'] != $old_password){
                $return['state'] = 200;
                $return['msg']   = '原密码错误';
                echo json_encode($return);
                exit;
            }
            if($password != $password1){
                $return['state'] = 200;
                $return['msg']   = '两次密码输入不一致';
                echo json_encode($return);
                exit;
            }

            if(strlen($password)<8){
                $return['state'] = 200;
                $return['msg']   = '密码安全度不够';
                echo json_encode($return);
                exit;
            }
            $data['oper_password']  = sha1($password);
            $map['id'] = session('uid');
            $result = M('operation')->where($map)->save($data);

            if($result){
                $return['state'] = 100;
                $return['msg']   = '修改成功';
                session(null);
                $return['url']   = U('Service/Login/index');
            }else{
                $return['state'] = 200;
                $return['msg']   = '修改失败';
            }
            echo json_encode($return);
            exit;
        }
        $this->user = $user;
        $this->display();
    }

}